<?php


namespace addon\hotel\model;

use app\model\BaseModel;

class HotelOrderCalc extends BaseModel
{
    /**
     * 店铺订单计算(支付完成后计算)
     * @param unknown $order_id
     */
    public function calculate($order_id)
    {
        $order_info = model("hotel_order")->getInfo([ [ 'order_id', '=', $order_id ] ], 'order_id,hotel_id,room_money,real_room_money');

        $hotel_info = model('hotel')->getInfo(['hotel_id'=>$order_info['hotel_id']],'commission_rate');

        $goods_money = $order_info[ 'real_room_money' ];
        $platform_money = round(floor($goods_money * $hotel_info[ 'commission_rate' ]) / 100, 2);
        $hotel_money = round(floor(( $goods_money - $platform_money ) * 100) / 100, 2);

        //todo  将订单发票的金额加入  店铺获取结算佣金字段中  平台优惠券金额(平台优惠券  平台承担部分 需要在结算的时候,讲平台承担的金额部分累加到店铺)
        model("hotel_order")->update([ 'hotel_money' => $hotel_money , 'platform_money' => $platform_money ], [ [ 'order_id', '=', $order_id ] ]);
        return $this->success();
    }

    /**
     * 订单退款金额累加计算
     * @param $order_money
     */
    public function refundCalculate($order_goods_info)
    {
        $order_id = $order_goods_info[ "order_id" ];

        //订单信息
        $order_info = model("hotel_order")->getInfo([ [ 'order_id', '=', $order_id ] ], 'hotel_money, platform_money, refund_money, refund_hotel_money, refund_platform_money, commission');
        if (empty($order_info))
            return $this->error([], "ORDER_EMPTY");

        $refund_money = $order_info[ "refund_money" ];//订单总退款
        $refund_shop_money = $order_info[ "refund_hotel_money" ];//订单退款 店铺金额
        $refund_platform_money = $order_info[ "refund_platform_money" ];//订单退款  平台金额

        $order_data = array (
            "refund_money" => $refund_money,
            "refund_shop_money" => $refund_shop_money,
            "refund_platform_money" => $refund_platform_money
        );

        $result = model("hotel_order")->update($order_data, [ [ 'order_id', '=', $order_id ] ]);
        return $this->success();

    }

    /**
     * 整体计算订单
     * @param unknown $out_trade_no
     * @return multitype:string
     */
    public function orderCalculate($out_trade_no)
    {
        $order_list = model("hotel_order")->getList([ [ 'out_trade_no', '=', $out_trade_no ] ], 'order_id');
        foreach ($order_list as $k => $v) {
            $this->calculate($v[ 'order_id' ]);
        }
        return $this->success();
    }
}